package com.timeriver.machine_learning.clustering

import org.apache.spark.ml.PipelineModel
import org.apache.spark.ml.feature.LabeledPoint
import org.apache.spark.ml.linalg.Vectors
import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}

object KmeansPredict {
  def main(args: Array[String]): Unit = {

    val session: SparkSession = SparkSession.builder()
      .master("local[6]")
      .appName("加载训练好的模型进行预测")
      .getOrCreate()

    import session.implicits._

    val iris: Dataset[String] = session.read
      .textFile("D:\\workspace\\gitee_space\\spark-ml-machine-learning\\data\\iris.data")
    iris.show(5, false)

    val data: Dataset[LabeledPoint] = iris.map(_.trim)
      .filter(!_.isEmpty)
      .map(line => {
        val strings: Array[String] = line.split(",")

        LabeledPoint(-1, Vectors.dense(
          strings(0).toDouble,
          strings(1).toDouble,
          strings(2).toDouble,
          strings(3).toDouble
        ))
      })

    val model: PipelineModel = PipelineModel.load("./model/kmeans")

    val frame: DataFrame = model.transform(data)

    frame.show(5, false)

    session.stop()
  }
}
